package com.vip.common.util;

import com.vip.common.constant.PublicConsts;

import java.util.UUID;

/**
 * 系统工具类
 *
 * @author vip
 * @date 2019/12/03 17:23
 */
public class PublicUtils {

    /**
     * 自动生成用户名
     *
     * @return
     */
    public static String generatorUsername() {
        return PublicConsts.PREFIX + (int) ((Math.random() * 9 + 1) * 100000);
    }

    /**
     * 手机号验证
     *
     * @param phoneNumber
     * @return
     */
    public static boolean phoneVerify(String phoneNumber) {
        String phoneRegular = "^1[3|4|5|7|8][0-9]\\d{4,8}$";
        return phoneNumber.length() == 11 && phoneNumber.matches(phoneRegular);
    }

    /**
     * 自动生成账号
     *
     * @return
     */
    public static String account() {
        String uuid = UUID.randomUUID().toString().substring(30);
        char[] chars = uuid.toCharArray();
        int clen = chars.length;
        StringBuffer result = new StringBuffer();
        for (int i = 0; i < clen; i++) {
            int num = chars[i];
            if (num > 64 && num < 123) {
                result.append(chars[i] - 64);
            } else {
                result.append(chars[i]);
            }
        }
        return result.toString();
    }

    /**
     * 查看source中包含多少个target
     *
     * @param source
     * @param target
     * @return
     */
    public static int containNum(String source, String target) {
        int he = 0;
        for (int i = 0; i < source.length(); i++) {
            int t = source.indexOf(target, i);
            if (i == t) {
                he++;
            }
        }
        return he;
    }
}